<?php

namespace App\Listeners\OpenShop;

use App\Events\OpenShop\OrderClosedEvent;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderLogModel;
use App\Service\OpenShop\User\RmsUserService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class OrderClosedLogListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Handle the event.
     *
     * @param OrderClosedEvent $event
     * @return void
     */
    public function handle(OrderClosedEvent $event)
    {
        $log = new OpenShopSignOrderLogModel;
        $log->orderId = $event->order->id;
        switch ($event->type) {
            case 1:
                $log->content = '自动关闭开店订单';
                $log->operatorName = '系统';
                break;
            case 2:
                $body = RmsUserService::getRmsUserInfoByUserIds([$event->uid]);
                $username = $body['data'][0]['username'] ?? "用户ID：{$event->uid}";

                $log->content = "关闭开店订单";
                $log->operatorName = $username;
                $log->createdByUid = $event->uid;
                $log->updatedByUid = $event->uid;
                break;
        }
        $log->type = $event->type;
        $log->save();
    }
}
